package ophiux.wechatpay.sdk.common.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import ophiux.wechatpay.sdk.OphiuxWeChatPaySDKException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLUtil
{
	/**
	 * 
	 * @param inputstream 
	 * @return  Document
	 * @throws OphiuxWeChatPaySDKException 
	 */
	public static Document buildDocument(InputStream inputstream) throws OphiuxWeChatPaySDKException{
		Document document = null;
		try {
			DocumentBuilder documentBuilder = 
					DocumentBuilderFactory.newInstance().newDocumentBuilder();
 
			try {
				document = documentBuilder.parse(inputstream);
			} catch (SAXException e) {
				throw new OphiuxWeChatPaySDKException("document parse error",e);
			} catch (IOException e) {
				throw new OphiuxWeChatPaySDKException("file read error",e);
			}
		} catch (ParserConfigurationException e) {
			throw new OphiuxWeChatPaySDKException("ParserConfigurationException",e);
		}
		return document;
	}
	
	/**
	 * ���xml�ַ�ת������ȡelement
	 * @param xmlStr   xmlԴ�ַ�
	 * @return
	 * @throws OphiuxWeChatPaySDKException 
	 */
	public static Element getDocmentElement(String xmlStr) throws OphiuxWeChatPaySDKException{
		return  buildDocument(getInputStream(xmlStr)).getDocumentElement();
	}
	
	/**
	 * ���xml�ַ����inputStream
	 * @param xmlStr
	 * @return
	 * @throws Exception 
	 */
	public static InputStream getInputStream(String xmlStr)  throws OphiuxWeChatPaySDKException{
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(xmlStr.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return is;
	}
	
	/**
	 * ���xml��ǩ��ƻ�ȡ����element
	 * @param parent  ����element
	 * @param tagName ��ǩ��
	 * @return List<Element> �����б�
	 */
	public static List<Element> getChildElements(Element parent,String tagName){
		List<Element> childelementList = new ArrayList<Element>();
		//��ݱ�ǩ���ȡ���е�����
		NodeList nodes = parent.getElementsByTagName(tagName);
		//ѭ���ӽڵ��ȡ��Ӧ�ڵ��element
		for(int i = 0;i<nodes.getLength();i++){
			Node node = nodes.item(i);
			Element element = (Element) node;
			childelementList.add(element);
		}
		return childelementList;
	}
	
	/**
	 * ��ݱ�ǩ���ȡelement�ڵ�
	 * @param parent  ���ڵ�
	 * @param tagName ��ǩ��
	 * @return Element
	 */
	public static Element getChildElement(Element parent,String tagName){
		Element element = null;
		List<Element> childElementList = getChildElements(parent, tagName);
		if(ListUtil.isNotBlank(childElementList)){
			element = childElementList.get(0);
		}
		return element;
	}
	
	/**
	 * ��ݸ��ڵ�ͱ�ǩ��ȡ��ǩֵ
	 * @param parent  ���ڵ�
	 * @param tagName ��ǩ
	 * @return
	 */
	public static Object getElementValue(Element parentElement,String tagName){
		Object value = null;
		Element element = getChildElement(parentElement, tagName);
		NodeList nodes = element.getChildNodes();
		if(nodes != null && nodes.getLength()>0){
			value = nodes.item(0).getNodeValue();
		}
		return value;
	}
	
	/**
	 * 
	 * @param element
	 * @return
	 */
	public static Object getElementValue(Element element){
		Object value = null;
		NodeList nodes = element.getChildNodes();
		if(nodes != null && nodes.getLength()>0){
			value = nodes.item(0).getNodeValue();
		}
		return value;
	} 
	
	public static void main(String[] args) {
		
	}
	
}
